#!/bin/bash
################### 备份数据库脚本 ################
# 执行之前：
# 	1. 去除特殊字符：sed -i 's/\r//' backup_db.sh
# 	2. 添加执行权限：chmod +x backup_db.sh
# 执行命令：./backup_db.sh
# 执行之后：
# 脚本说明：使用mysqldump工具每天备份mysql的部分表数据。
#########################################################

HOST=pc-2zeynsw1bqyi91362.mysql.polardb.rds.aliyuncs.com
PORT=3306
DB=stock_db_formal
USER=stock_db_formal
PASS=stock@W1qL
DATE=$(date '+%Y%m%d')
BACKUP_DIR_BASE=/opt/data/mysql_backup
BACKUP_DIR=$BACKUP_DIR_BASE/${DATE}
TABLE_LIST="t_admin t_comb_basic t_comb_component t_comb_follow t_comb_stat t_comb_tx_detail t_fund_basic t_fund_manager_detail t_fund_manager_file t_hk_stock_basic t_news t_score_detail t_stock_basic t_stock_daily t_stock_blacklist t_strategy_pr_count t_strategy_click_detail t_user t_user_level xxl_job_group xxl_job_info xxl_job_lock xxl_job_log xxl_job_log_report xxl_job_logglue xxl_job_registry xxl_job_user"
echo "TABLE_LIST: $TABLE_LIST"

[ ! -d $BACKUP_DIR ] && mkdir -p $BACKUP_DIR &>/dev/null
echo "mkdir success: $BACKUP_DIR"

for TABLE in $TABLE_LIST
do
         echo "-------begin backup table: $TABLE-------"
         BACKUP_NAME=$BACKUP_DIR/${TABLE}.sql
         if mysqldump -h$HOST -P$PORT -u$USER -p$PASS $DB $TABLE > $BACKUP_NAME 2>/dev/null; then
             echo "success backup table: $TABLE"
         else
	echo "fail backup table: $TABLE"
         fi 
 done
cd $BACKUP_DIR_BASE
zip -r ${DATE}.zip ${DATE}/
rm -rf ${DATE}/
echo "backup success $DATE"